﻿@{
    ViewBag.Title = "休假统计";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
}
<div class="main layui-clear">
    <div class="wrap">
        <div class="layui-form layui-form-pane">
            <fieldset class="layui-elem-field">
                <legend>休假统计列表</legend>
                <div class="layui-field-box">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">休假年份</label>
                            <div class="layui-input-block">
                                <input id="Year" name="Year" class="layui-input" readonly="readonly" value="@DateTime.Now.Year">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">休假标题</label>
                            <div class="layui-input-block">
                                <input class="layui-input" id="title">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item" style="float:right">
                        <a href="#" class="layui-btn layui-btn-normal" id="btnSearch">
                            <i class="layui-icon layui-icon-search"></i>检索
                        </a>
                        <a href="@Url.Action("Add","Count")" class="layui-btn layui-btn-normal IsAdmin">发布休假统计</a>
                    </div>
                </div>
            </fieldset>
        </div>
        <table id="holiday" lay-filter="holiday"></table>
        <script type="text/html" id="toolbar">
            <b>休假信息一览</b>
        </script>
        <script type="text/html" id="TitleTpl">
            <a href="#" class="layui-table-link" lay-event="edit">{{d.Title || ''}}</a>
        </script>
        <script type="text/html" id="barDemo">
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="list">社员休假明细</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger IsAdmin" lay-event="del">删除</a>
        </script>
    </div>
</div>

@section scripts{
    <script>
        layui.use(['table', 'jquery'], function () {
            var $ = layui.jquery;
            var table = layui.table;
            if (!layui.data('user').user.IsAdmin) {
                $(".IsAdmin").css("display", "none");
            }
            table.render({
                elem: '#holiday'
                , height: 500
                , title: '休假列表'
                , url: '/Count/CountList'
                , autoSort: false
                , page: {}
                , limits: [10]
                , toolbar: '#toolbar'
                , defaultToolbar: ['filter', 'exports', 'print']
                , cols: [[
                    { type: 'numbers' }
                    , { field: 'Id', title: 'ID', hide: true }
                    , { field: 'Title', title: '休假标题', width: 240, templet: '#TitleTpl' }
                    , { field: 'StartDate', title: '开始时间', width: 120}
                    , { field: 'EndDate', title: '结束时间', width: 120 }
                    , { field: 'Days', title: '休假天数', width: 100, templet: '<span>{{ d.Days }} 天</span>' }
                    , { field: 'OutDate', title: '是否过期', width: 100 }
                    , { fixed: 'right', title: '操作', toolbar: '#barDemo', width: 180 }
                ]]
            });

            table.on('tool(holiday)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    layer.confirm('真的删除行么', function (index) {
                        $.ajax({
                            url: '/Count/DelCount?hid=' + data.Id,
                            type: 'post',
                            contentType: false,
                            processData: false,
                            dataType: "json",
                            success: function (res) {
                                if (res.status == 0) {
                                    layer.msg("删除成功", { icon: 1 });
                                    obj.del();
                                    layer.close(index);
                                } else if (res.status == 1) {
                                    layer.msg(res.msg, { icon: 5 });
                                }
                            }
                            , error: function (res) {
                                layer.msg("请求异常", { icon: 5 });
                            }
                        });
                    });
                } else {
                    if (data.OutDate == "是") {
                        layer.msg("该休假信息已经过期", { icon: 2 });
                        return;
                    }                    
                    if (obj.event === 'edit') {
                        redirect('Count', 'Detail', data.Id);                     
                    } else if (obj.event === 'list') {
                        redirect('Count', 'AllList', data.Id);                          
                    }
                }
            });

            $("#btnSearch").click(function () {
                var name = $("#title").val();
                table.reload('holiday', {
                    page: { curr: 1 }
                    , url: '/Count/CountList?name=' + name
                });
            });
        });
    </script>
}